<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller
{

    public function index(){
        $this->display();
    }

    public function loginz(){

        $account = I('post.user_name');
        $password = getPwdEncodeString(I('post.password'));

        

//        $user = M('user');
        $user = M('Manage');
        $user_info = $user->where(['account' => $account])->find();

        if($user_info === null){

            $ajaxReturn['code'] = 501;
            $ajaxReturn['msg'] = "账号不存在！";

        }else if($user_info['password'] != $password){

            $ajaxReturn['code'] = 502;
            $ajaxReturn['msg'] = "密码错误！";

        }else if($user_info['account_status'] == 0){

            $ajaxReturn['code'] = 503;
            $ajaxReturn['msg'] = "该账号已被禁止登录！";

        }else{

            $ajaxReturn['code'] = 200;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data']['user_name'] = $user_info['user_name'];
            $ajaxReturn['data']['role_id'] = $user_info['role_id']; 
            session('user_info', $user_info);
            $data['last_login'] = time();
            $userid = $user_info['user_id'];

            $user->where('user_id='.$userid)->save($data);
            $user->where('user_id='.$userid)->setInc('login_count',1);
            
        }
        $this->ajaxReturn($ajaxReturn);

    }

 

    public function logout(){
         
        session('user_info',null);

        if(!session('?user_info')){
            $result['msg']='成功退出';
            $result['code']=200;
        }else{
            $result['msg']='退出失败';
            $result['code']=500;
        }

        $this->ajaxReturn($result);
 
    }

    

    public function resetPassword(){

        $this->isAjaxLogin();//检查是否登录

        $oldpwd	= getPwdEncodeString(I('post.oldpwd'));
        $newpwd  = getPwdEncodeString(I('post.newpwd'));

        $userinfo = session('user_info');

        if($oldpwd != $userinfo['password']){

            $ajaxReturn['code'] = 500;
            $ajaxReturn['msg']  = '旧密码错误!';
        }else{

            $Model = M('Manage');

            $userid = $userinfo['id'];

            $data['password'] = $newpwd;

            $Model->where('id='.$userid)->save($data);

            $ajaxReturn['code'] = 200;
            $ajaxReturn['msg']  = '设置成功!';
        }

        $this->ajaxReturn($ajaxReturn);
    }

 


}